<?php

/**
 * @version		$Id: vote.php 21097 2011-04-07 15:38:03Z dextercowley $
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access.
defined('_JEXEC') or die;


jimport('joomla.plugin.plugin');

/**
 * Facebook like plugin.
 *
 * @package		Joomla.Plugin
 * @subpackage	Content.vote
 */
class plgContentFviralbox extends JPlugin {

	/**
	 * Constructor
	 *
	 * @access      protected
	 * @param       object  $subject The object to observe
	 * @param       array   $config  An array that holds the plugin configuration
	 * @since       1.5
	 */
	public function __construct(& $subject, $config) {
		parent::__construct($subject, $config);
		$this->loadLanguage();
	}

	/**
	 * @since	1.6
	 */
	public function onContentBeforeDisplay($context, &$row, &$params, $page = 0) {
		if ($context !== 'com_content.article')
			return;
		static $script = null;
		$url = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug), true, -1);
		if (empty($url))
			return;

		if (is_null($script)) {
			$app = JApplication::getInstance("site");
			$doc = JFactory::getDocument();

			$fb_appid = $this->params->get('fb_appid');
			// Prevent multiple FB script instances
			if (!isset($doc->with_fbscript) || !$doc->with_fbscript) {
				$script = <<< EOF
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=$fb_appid";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
EOF;
				$doc->addScriptDeclaration($script);
				$doc->with_fbscript = true;
			}
			$color_scheme = $this->params->get('color_scheme') ? 'dark' : 'light';
			$width = $this->params->get('likebox_width');
			$show_faces = $this->params->get('likebox_show_faces') ? 'true' : 'false';
			$action = $this->params->get('likebox_verb') ? 'recommend' : 'like';
			$send_enabled = $this->params->get('likebox_send_enabled') ? 'true' : 'false';
		}
		$html = <<< EOF
<div class="fb-like" data-href="$url" data-send="$send_enabled" data-layout="button_count" data-width="$width" data-show-faces="$show_faces" data-font="arial" data-action="$action" data-colorscheme="$color_scheme"></div>
EOF;
		return $html;
	}

	public function onContentAfterDisplay($context, &$row, &$params, $page = 0) {
		$doc = JFactory::getDocument();
		if (!isset($doc->fb_root) || !$doc->fb_root) {
			$doc->fb_root = true;
			return '<div id="fb-root"></div>';
		}
	}

	public function onContentBeforeSave($context, &$table, $isNew) {
		
	}

	public function onContentPrepare($context, &$row, &$params, $page = 0) {
		if ($context !== 'com_content.article')
			return;

		$doc = JFactory::getDocument();
		// Will move Open Graph to a system plugin
		$fb_appid = $this->params->get('fb_appid');
		$site_name = JFactory::getConfig()->getValue('config.sitename');
		if (JPluginHelper::importPlugin('content', 'xprop')
				&& plgContentXprop::getExtendedProperty($row->id, 'xprop_small', $image)) {
			$image = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $image;
		} else {
			$image = 'http://' . $_SERVER['HTTP_HOST'] . '/images/site-logo.png';
		}
		$url = JURI::current(); // $_SERVER['host'].JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug));

		$doc->open_graph = array('fb' => array(), 'og' => array(), 'article' => array());
		$doc->open_graph['fb']['app_id'] = $fb_appid;
		$doc->open_graph['og']['type'] = 'article';
		$doc->open_graph['og']['url'] = $url;
		$doc->open_graph['og']['site_name'] = $site_name;
		$doc->open_graph['og']['image'] = $image;
		$doc->open_graph['og']['title'] = $row->title;
		$doc->open_graph['og']['type'] = 'article';
		$doc->open_graph['og']['description'] = strip_tags($row->introtext);
		if ($row->publish_up !== '0000-00-00 00:00:00')
			$doc->open_graph['article']['published_time'] = $row->publish_up;
		if ($doc->getMetaData('author'))
			$doc->open_graph['article']['author'] = $doc->getMetaData('author');
		if ($row->publish_down !== '0000-00-00 00:00:00')
			$doc->open_graph['article']['expiration_time'] = $row->publish_down;
		if ($doc->getMetaData('keywords'))
			$doc->open_graph['article']['tag'] = $doc->getMetaData('keywords');
	}

}
